// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from "element-ui"
//导入element css
import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'
import store from './store/index'

import Blob from './excel/Blob'
import Export2Excel from './excel/Export2Excel'

// 注册全局钩子用来拦截导航
router.beforeEach((to, from, next) => {
  const token = store.state.token
  if (to.path === '/') return next();
  if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
    if (token) { // 通过vuex state获取当前的token是否存在
      next()
    } else {
      console.log('该页面需要登陆')
      next({
        path: '/'
        // query: {redirect: to.fullPath} // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  } else {
    next()
  }
})

window.addEventListener('popstate', function() {
  history.pushState(null, null, document.URL)
})

Vue.use(router);
Vue.use(ElementUI);
Vue.config.productionTip = false;
// 引用axios，并设置基础URL为后端服务api地址;
axios.defaults.baseURL = "http://localhost:8080/";
//设置全局，每次ajax请求携带cookies
 axios.defaults.withCredentials = true
// 将API方法绑定到全局
Vue.prototype.$axios = axios
import moment from 'moment'
Vue.filter('dataFormat',function (dataStr,pattern="YYYY-MM-DD") {
  return moment(dataStr).format(pattern)
})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
